package com.dbgs.insurance.insuredperson.activity.register;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.dbgs.insurance.insuredperson.R;
import com.dbgs.insurance.insuredperson.activity.MainActivity;
import com.dbgs.insurance.insuredperson.constants.ConstInt;
import com.hss.base.BaseActivty;
import com.tuo.customview.VerificationCodeView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/14.
 */

public class InputVerifyCodeActivity extends BaseActivty {

    @BindView(R.id.return_img)
    AppCompatImageView returnImg;
    @BindView(R.id.send_phone)
    AppCompatTextView sendPhone;
    @BindView(R.id.verify_code_view)
    VerificationCodeView verifyCodeView;
    @BindView(R.id.again_verify)
    AppCompatTextView againVerify;


    public static final String FLAG = "flag"; // 1:登录 2；注册
    public int mFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_verify_code);
        ButterKnife.bind(this);
        initData();
        initView();
        initListener();
    }

    @Override
    public void initData() {
        super.initData();
        if (getIntent() != null) {
            mFlag = getIntent().getIntExtra(FLAG, 0);
        }
    }

    @Override
    public void initView() {
        super.initView();
        showKeyboard();
        sendPhone.setText(getResources().getString(R.string.str_send_phone)+"1582432156");
        timer.start();
    }

    private void showKeyboard() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(verifyCodeView, 0);
    }

    private void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }


    @Override
    public void initListener() {
        super.initListener();

        verifyCodeView.setInputCompleteListener(new VerificationCodeView.InputCompleteListener() {
            @Override
            public void inputComplete() {
                //showToast(verifyCodeView.getInputContent());
                if (verifyCodeView.getInputContent().length() == 6 && checkVerifyCode()) {
                    if (mFlag == 1) {
                        setResult(ConstInt.RESULT_VALUE_CHANGED);
                        startActy(MainActivity.class);
                        finishActivity();
                    } else {
                        startActyForResult(RegisterInfoActivity.class, ConstInt.REQUEST_REGISTER_CODE);
                    }
                }
            }

            @Override
            public void deleteContent() {
                showToast("icv_delete" + verifyCodeView.getInputContent());
            }
        });

    }
    private boolean checkVerifyCode(){
        //
        return true;
    }

    private CountDownTimer timer = new CountDownTimer(60000,1000) {
        @Override
        public void onTick(long l) {
            againVerify.setEnabled(false);
            againVerify.setText((l/1000)+"秒后可重发");
        }

        @Override
        public void onFinish() {
            againVerify.setEnabled(true);
            againVerify.setText(R.string.str_again_verify);
        }
    };

    @OnClick({R.id.return_img, R.id.again_verify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.return_img:
                exit();
                break;
            case R.id.again_verify:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==ConstInt.REQUEST_REGISTER_CODE && resultCode==ConstInt.RESULT_VALUE_CHANGED){
            setResult(resultCode);
            exit();
        }
    }
}
